package com.cloud.jdkTest;

import org.junit.Test;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.NumberFormat;

public class BigdecimalTest {
    @Test
    public void mathTest(){
        double b1 = 0.3;
        double b2 = 4.2;
        double b3 = 0.1;
        double bb = 0.2;
        double b4 = b2 - b1;
        double b5 = b2 / b1;
        double b6 = bb + b3;
        double b7 = b3 * bb;
        System.out.println("b4 = " + b4);
        System.out.println("b5 = " + b5);
        System.out.println("b6 = " + b6);
        System.out.println("b7 = " + b7);
    }

    @Test
    public void DecimalTest(){
        BigDecimal b1 = new BigDecimal(0.1);
        BigDecimal b2 = new BigDecimal("0.1");
        System.out.println("b1 = " + b1);
        System.out.println("b2 = " + b2);
    }

    @Test
    public void arithmeticTest(){
        BigDecimal b1 = BigDecimal.valueOf(0.3);
        BigDecimal b2 = BigDecimal.valueOf(4.2);
        BigDecimal b3 = BigDecimal.valueOf(0.1);
        BigDecimal b4 = BigDecimal.valueOf(0.2);
        System.out.println("b2 - b1 = " + b2.subtract(b1));
        System.out.println("b3 + b4 = " + b3.add(b4));
        System.out.println("b3 * b4 = " + b3.multiply(b4));
        System.out.println("b2 / b1 = " + b2.divide(b1));
        /**
         * 该类进行除法运算时，如果出现无法整除的情况.该类会抛出
         * java.lang.ArithmeticException:
         * Non-terminating decimal expansion;
         * no exact representable decimal result.
         */
//        System.out.println("10.1 / 3.1 = " + BigDecimal.valueOf(10.1).divide(BigDecimal.valueOf(3.1)));
        System.out.println("10.1 / 3.1 = " + (10.1 / 3.1));
        System.out.println("10.1 / 3.1 = " + BigDecimal.valueOf(10.1).divide(BigDecimal.valueOf(3.1),2, RoundingMode.HALF_UP)); // 四舍五入
        System.out.println("10.1 / 3.1 = " + BigDecimal.valueOf(10.1).divide(BigDecimal.valueOf(3.1),2, RoundingMode.UP));  // 向上取整
        System.out.println("10.1 / 3.1 = " + BigDecimal.valueOf(10.1).divide(BigDecimal.valueOf(3.1),2, RoundingMode.CEILING)); // 向上取整
        System.out.println("10.1 / 3.1 = " + BigDecimal.valueOf(10.1).divide(BigDecimal.valueOf(3.1),2, RoundingMode.DOWN)); // 向下取整
        System.out.println("10.1 / 3.1 = " + BigDecimal.valueOf(10.1).divide(BigDecimal.valueOf(3.1),2, RoundingMode.FLOOR));// 向下取整
    }

    @Test
    public void formatTest(){
        // 创建货币格式化对象
        NumberFormat currencyInstance = NumberFormat.getCurrencyInstance();
        // 创建百分比格式化对象
        NumberFormat percentInstance = NumberFormat.getPercentInstance();
        // 设置百分比最大的小数点位数为3位
        percentInstance.setMaximumFractionDigits(3);
        BigDecimal capital = BigDecimal.valueOf(10000.00);
        BigDecimal rate = BigDecimal.valueOf(0.15123);
        BigDecimal interest = capital.multiply(rate);
        System.out.println("本金为: " + currencyInstance.format(capital));
        System.out.println("利率为: " + percentInstance.format(rate));
        System.out.println("利息为: " + currencyInstance.format(interest));
    }
}
